define(function(require,exports,module) {
    require('static_v4/js/aes');
    var myPub = require('./public');
    var oActive = $('.js_active');                      // 活动规则按钮
    var oActiveCon = $('.js_activeCon').html();         // 活动规则内容部分
    var oActiveTit = $('.js_activeTit').html();         // 活动规则标题
    var oRegister = $('.js_register');                  // 点击注册按钮
    var oInviteRegTit = $('.js_inviteRegTit').html();   // 注册标题
    var oInviteRegCon = $('.js_inviteRegCon').html();   // 注册内容
    var oFailCon = $('.js_failCon').html();             // 注册失败内容

    var oInviteSend = $('.js_inviteSend');              // 发送我的专属链接按钮
    var oInviteShadow = $('.js_inviteShadow');          // 引导分享层
    var oInviteBg = $('.js_inviteBg');                  // 背景图层

    // 点击弹出指导分享层
    oInviteBg.hammer().on('tap', function () {
        oInviteShadow.fadeIn('slow');
    });
    oInviteSend.hammer().on('tap',function (e) {
        oInviteShadow.fadeIn('slow');
        e.preventDefault()
    });
    oInviteShadow.hammer().on('tap',function (e) {
        $(this).fadeOut('slow');
        e.preventDefault()
    });
    // 活动规则
    oActive.hammer().on('tap',function () {
        myPub.TipBtnLayer(oActiveCon,oActiveTit,'确认',function () {
            myPub.LayerCloseAll();
        },function () {
            myPub.LayerCloseAll();
        },function () {
            $('.layermbox').addClass('activeRule');
            $('.layermbox h3').addClass('brown')
        })
    });
    // 注册
    oRegister.hammer().on('tap',function () {
        TipBtnLayer(oInviteRegCon,oInviteRegTit,'确认',function () {
            var oLayermcont = $('.layermcont');
            var oErrTip = oLayermcont.find('.js_errorTip p');    // 错误提示
            //提交表单前操作 检验格式
            //fnEmpty:输入框有未填     fnError:错误格式   fnRight：符合格式
            function SublimtBefor (fnEmpty, fnError, fnRight) {
                var _offBtn = 0;
                var _iNow;
                var oInputBoxList = oLayermcont.find('.js_inputBoxList');
                for(var i = 0; i < oInputBoxList.find('.js_Text').length; i++) {
                    if(oInputBoxList.find('.js_Text').eq(i).val() == '') {
                        _offBtn++;
                        _iNow = i;
                        break;
                    }
                }

                //没有空密码
                if(_offBtn == 0) {
                    //错格式密码提示
                    if(oInputBoxList.find('.has_error').length > 0) {
                        var _errorNum = oInputBoxList.find('.js_login_input').index(oInputBoxList.find('.has_error'));
                        //格式不正确
                        if(fnError) fnError(_errorNum)
                        //myPub.TipLayer('请输入6-20位数字或者英文字母的密码格式');
                    } else {
                        //没有错误
                        if(fnRight) fnRight()
                    }

                } else { // 有空密码
                    if(fnEmpty) fnEmpty(_iNow);
                    //myPub.TipLayer('密码不能为空');
                }
            }
            //前端效验完成
            function SublimtAfter (callBack) {
                    //图形验证码隐藏
                    SublimtBefor(function(index) {
                        switch(index) {
                            case 0:
                                oErrTip.text('手机号码不能为空！');
                                break;
                            case 1:
                                oErrTip.text('请输入右边验证码！');
                                break;
                            case 2:
                                oErrTip.text('动态验证码不能为空！');
                                break;
                            case 3:
                                oErrTip.text('请输入密码！');
                                break;

                        }
                    }, function(index) {
                        switch(index) {
                            case 0:
                                oErrTip.text('请输入正确的手机号码！');
                                break;
                            case 1:
                                oErrTip.text('请输入正确的验证码！');
                                break;
                            case 2:
                                oErrTip.text('请输入正确的动态验证码！');
                                break;
                            case 3:
                                oErrTip.text('请输入正确的密码！');
                                break;
                        }

                    }, function() {
                        if(callBack)callBack()
                    });
            }
            SublimtAfter (function () {
                oErrTip.text(' ');
                var oldMemberId = $('#oldMemberId').val();
                var activityId = $('#activityId').val();

                $.ajax({
                    url: '/ddjLoginAuth/registerForActivity',
                    data: {
                        "oldMemberId": oldMemberId,
                        "activityId":activityId,
                        "mobile": $('.layermcont').find('.js_loginPhoneNum').val(),
                        "password":$('.layermcont').find('#password').val(),
                        "smsCode" : $('.layermcont').find('.js_zhuceCode').val()
                    },
                    type: 'POST',
                    dataType: 'json',
                    success: function(data) {
                        if(data.code == 'S_000') {
                            myPub.TipLayer(data.msg);
                            document.getElementById("js_goHome").click();
                            //window.location.href="/home";
                            sessionStorage.setItem('judgeNew_friend', 'newFriend');
                            var newFriendCoup = JSON.stringify(data.d);
                            sessionStorage.setItem('judgeNew_friend_coupons', newFriendCoup);
                            myPub.LayerCloseAll();
                        } else {
                            oErrTip.text(data.msg);
                        }
                    },
                    error:function(x){
                        if(403==x.status){
                            window.location.href="/loginExpired";
                        }
                        console.log(x.status);
                    }
                });

            });

        },function () {
            myPub.LayerCloseAll();
        },function () {
            $('.layermbox').addClass('activeRule');
            $('.layermbox h3').addClass('brown');
            $('.laymshade').addClass('removeFixed');
            $('.layermmain').addClass('removeFixed');
            var oLayermcont = $('.layermcont');
            var oLoginPhoneNum = oLayermcont.find('.js_loginPhoneNum'); 	// 手机号码框
            var oGetCode = oLayermcont.find('.js_getCode');                 // 验证码按钮
            var oGetCodeDisabled = oLayermcont.find('.js_getCodeDisabled'); // 验证码倒计时
            var oClearBtn = oLayermcont.find('.js_clearBtn'); 				// 清理按钮
            var oShowPwd = oLayermcont.find('.js_showPwd'); 				// 显示和隐藏密码
            var oLoginPassWord = oLayermcont.find('.js_loginPassWord'); 	// 密码输入
            var oYzmInput = oLayermcont.find('.js_yzm'); 					// 验证码输入框
            var oErrTip = oLayermcont.find('.js_errorTip p');               // 错误提示
            var oGetCodeImg = oLayermcont.find('.js_getCodeImg');           // 刷新图形验证码区域
            var oCode = oLayermcont.find('.js_code');                    // 图形验证码输入框
            /*var oJtext = oLayermcont.find('.js_Text');                                    // 输入框
            var psdScrollTap_timer = null;
            $('.layermbox').on('click',function (object) {
                if($(":focus").length == 0){
                    clearTimeout(psdScrollTap_timer);
                    psdScrollTap_timer = setTimeout(function () {
                        window.scrollTo(0,document.documentElement.clientHeight);
                    },100);
                }

            });*/

            //清除按钮操作
            oClearBtn.hammer().on('tap', function() {
                $(this).parents('.js_loginMob').removeClass('has_error').removeClass('has_success');
                $(this).parents('.js_loginMob').find('.js_Text').val('');
                $(this).hide();
            });
            //验证码显示清除按钮
            oYzmInput.on('input propertychange', function() {
                var _oClearBtn = $(this).siblings('.js_clearBtn');
                $(this).val() == '' ? _oClearBtn.hide() : _oClearBtn.show();
            });
            //密码输入检测
            oLoginPassWord.each(function(i) {
                oLoginPassWord.eq(i).on('input propertychange', function() {
                    userPassWord(oLoginPassWord.eq(i));

                    var _oClearBtn = $(this).siblings('.js_clearBtn');
                    $(this).val() == '' ? _oClearBtn.hide() : _oClearBtn.show();
                });

            });
            //密码显示和隐藏
            oShowPwd.hammer().on('tap', function() {
                if($(this).hasClass('on')) {
                    $(this).removeClass('on icon-eye_look').addClass('icon-eye_close').siblings('.js_loginPassWord').attr('type', 'password')
                } else {
                    $(this).addClass('on icon-eye_look').removeClass('icon-eye_close').siblings('.js_loginPassWord').attr('type', 'text')
                }
                myPub.HamstopPropaga();

            });
            //手机输入框
            oLoginPhoneNum.on('input propertychange', function() {
                phonemobile(oLoginPhoneNum);
                var _oClearBtn = $(this).siblings('.js_clearBtn');

                $(this).val() == '' ? _oClearBtn.hide() : _oClearBtn.show();
            });

            //密码输入检测
            oLoginPassWord.each(function(i) {
                oLoginPassWord.eq(i).on('input propertychange', function() {
                    userPassWord(oLoginPassWord.eq(i));

                    var _oClearBtn = $(this).siblings('.js_clearBtn');
                    $(this).val() == '' ? _oClearBtn.hide() : _oClearBtn.show();
                });

            });

            //发送验证码
            oGetCode.hammer().on('tap', function() {
                var oLoginMob = oLoginPhoneNum.parents('.js_loginMob');

                if(oLoginPhoneNum.val() == '') {
                    oErrTip.text('请输入手机号码！')
                } else if(oLoginMob.hasClass('has_error')) {
                    oErrTip.text('请输入正确的手机号码！')
                } else {
                    oErrTip.text(' ');
                    sendCode({"mobile": oLoginPhoneNum.val(),"type":1, "captch": Encrypt(oCode.val())},oGetCode);
                }
            });

            //发送验证码
            function sendCode(datas,obj) {
                $.ajax({
                    url: '/ddjLoginAuth/sendCode',
                    data: datas,
                    type: 'POST',
                    dataType: 'json',
                    success: function(data) {
                        if(data.code == 'S001') {
                            setCode(obj);

                        }
                        else if(data.code == 'F_001' || data.code == 'F002') {
                            refreshCode();
                            oErrTip.text(data.msg);
                        }
                        else{
                            //注册
                            if(data.code === 'F_003'){
                                //手机已注册
                                oErrTip.text('该手机号已注册');
                            }
                        }

                    },
                    error:function(x){
                        if(403==x.status){
                            window.location.href='/loginExpired';
                        }
                        console.log(x.status);
                    }
                });
            }

            /*验证手机号码*/
            function phonemobile(obj) {

                var oLoginMob = obj.parents('.js_loginMob');
                myPub.checkMobile(obj, function() {
                    oLoginMob.removeClass('has_error').removeClass('has_success');
                    //myPub.TipLayer('电话号码不能为空');
                }, function() {
                    oLoginMob.addClass('has_error').removeClass('has_success');
                    //myPub.TipLayer('不是手机号码,请重新谈填写');
                }, function() {
                     oLoginMob.addClass('has_success').removeClass('has_error');
                });
            }

            /*验证密码*/
            function userPassWord(obj) {
                var oLoginMob = obj.parents('.js_loginMob');
                myPub.checkPassWord(obj, function() {
                    oLoginMob.removeClass('has_error').removeClass('has_success');
                }, function() {
                    oLoginMob.addClass('has_error').removeClass('has_success');
                }, function() {
                    oLoginMob.addClass('has_success').removeClass('has_error');

                });
            }

            /*倒计时*/
            function setCode() {
                oGetCode.hide();
                oGetCodeDisabled.show();
                myPub.CutTime(60, function() {
                    oGetCodeDisabled.hide();
                    oGetCode.show().text('重新发送');
                }, function(time) {
                    oGetCodeDisabled.text(time + '秒');
                });
            }

            //点击切换图片验证码
            oGetCodeImg.hammer().on('tap',function() {
                var timestamp = (new Date()).valueOf();
                var imageSrc = oGetCodeImg.find('img').attr("src");

                if(imageSrc.indexOf("?") >= 0) {
                    imageSrc = imageSrc.substring(0, imageSrc.indexOf("?"));
                }
                imageSrc = imageSrc + "?timestamp=" + timestamp;
                oGetCodeImg.find('img').attr("src", imageSrc);
            });

            //刷新验证码
            function refreshCode(){
                var oGetCodeImg = oLayermcont.find('.js_getCodeImg');
                var timestamp = (new Date()).valueOf();
                var imageSrc = oGetCodeImg.find('img').attr("src");

                if(imageSrc.indexOf("?") >= 0) {
                    imageSrc = imageSrc.substring(0, imageSrc.indexOf("?"));
                }
                imageSrc = imageSrc + "?timestamp=" + timestamp;
                oGetCodeImg.find('img').attr("src", imageSrc);
            }

        })
    });

    // 领取失败
    function failGet() {
        myPub.TipBtnLayer(oFailCon,'no','去商城首页',function () {
            document.getElementById("js_goHome").click();
        },function () {
            myPub.LayerCloseAll();
        },function () {
            $('.layermbox').addClass('activeRule');
            $('.layermbox h3').addClass('brown');
        })
    }

    //在此处判断不是新用户情况下执行一下方法
    if($('#isNewUser').length > 0 && $('#isNewUser').val() != 'true'){ // 判断是不是需要弹窗的页面
         failGet();
    }
    // 返回首页跳转app功能
    myPub.JudgeAppHref($('#js_goHome'));
});

function Encrypt(word){
    var strInfo = "0102030405060708";
    var key = CryptoJS.enc.Utf8.parse(strInfo);
    var iv  = CryptoJS.enc.Utf8.parse(strInfo);
    var srcs = CryptoJS.enc.Utf8.parse(word);
    var encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv,mode:CryptoJS.mode.CBC});
    return encrypted.toString();
}
// 调取注册弹框
function TipBtnLayer (sCont,sTit,btnName,yesFn,cancelFn,fn) {
    layer.open({
        title: sTit != 'no' ? sTit : '',
        content: sCont,
        btn: btnName == undefined ? ['确定'] : [btnName],
        shadeClose: false,
        scrollbar: false,
        yes:function() {if(yesFn) yesFn();},
        cancel:function() {if(cancelFn) cancelFn();},
        success:function() {
            if(fn)fn();
            $('.layermbtn span').css({'width': '100%'})
        }
    });
   NoTouchMove($('.laymshade'));
}

function NoTouchMove (obj) {
    obj.on('touchmove',function() {return false;})
}